﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameWork.Source.Object_Classes;
using Microsoft.Xna.Framework;
using Boomer.Source.QuadTree;
using Boomer.Source.Character;
using FrameWork.Source.Utilities;
using Boomer.Source.TiledMap;

namespace Boomer.Source.WaveManager
{
    class WaveManager
    {
        private static WaveManager manager = null;

        public int currentWave { get; private set; }
        public float waveTime { get; private set; }
        public float nextWaveTime { get; private set; }
        public float monsterTime { get; private set; }
        public float nextMonsterTime { get; private set; }

        private CAnimatedObject doorObject;
        private List<Vector2> listMapPos;

        private CQuadTree quadTree;

        private bool isCreateMob;
        private int numMobInWave;

        private int totalMobs;

        private CTiledMap map;

        WaveManager()
        {
            currentWave = 1;
            waveTime = 0.0f;
            monsterTime = 0.0f;

            nextWaveTime = 7.0f;
            nextMonsterTime = 3.0f;
            isCreateMob = false;
            numMobInWave = currentWave * 2;
            totalMobs = numMobInWave;
            listMapPos = new List<Vector2>();
        }


        public static WaveManager getInstance()
        {
            if (manager == null)
                manager = new WaveManager();

            return manager;
        }

        public void init()
        {

        }

        public void setPos(int mapRow, int mapCol)
        {
            listMapPos.Add( new Vector2(mapRow, mapCol) );
        }

        public void attachQuadTree(CQuadTree quadtree)
        {
            this.quadTree = quadtree;
        }

        public void update(float deltaTime)
        {
            waveTime += deltaTime;
            if (waveTime >= nextWaveTime)
            {
                waveTime = 0.0f;
                currentWave += 1;
                nextWaveTime += 3.0f;
                numMobInWave = currentWave * 2;
                totalMobs += numMobInWave;
            }


            monsterTime += deltaTime;
            if (monsterTime > nextMonsterTime)
            {
                monsterTime = 0.0f;
                if (totalMobs > 0)
                    createMobs();
            }
        }

        public void attachMap(CTiledMap map)
        {
            this.map = map;
        }

        public void reset()
        {
            currentWave = 1;
            waveTime = 0.0f;
            monsterTime = 0.0f;

            nextWaveTime = 5.0f;
            nextMonsterTime = 1.0f;
            isCreateMob = false;
            numMobInWave = currentWave * 2;
            listMapPos = new List<Vector2>();
        }

        public void createMobs()
        {
            int place = CUtilities.getInstance().randomNumber(0, 4);
            int type = CUtilities.getInstance().randomNumber(0, 100);
            CMob pMob = null;
            if (type < 80)
            {
                int mobsType = CUtilities.getInstance().randomNumber(0, 3);
                pMob = new CMonster();
                ((CMonster)pMob).setMobType((CMonster.MOBTYPE)mobsType);
                pMob.init();
                pMob.attachMap(this.map);
                pMob.setPosInMap((int)this.listMapPos[place].X, (int)this.listMapPos[place].Y);
            }
            if( type < 20 )
            {
                int mobsType = CUtilities.getInstance().randomNumber(0, 2);
                pMob = new CRageMonster();
                ((CRageMonster)pMob).setMobType((CRageMonster.MOBTYPE)mobsType);
                pMob.init();
                pMob.attachMap(this.map);
                pMob.setPosInMap((int)this.listMapPos[place].X, (int)this.listMapPos[place].Y);
            }
            totalMobs -= 1;
            if( pMob != null )
                this.quadTree.addObject(pMob);
        }

        public void draw()
        {

        }
    }
}
